#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>
#include <pcap.h>

static void print_packet_bytes(const unsigned char *d, const size_t l)
{
	size_t i;
	for (i = 0; i < l; ++i)
	{
		if (i % 0x10 == 0)
		{
			if (i != 0)
				printf("\n");
			printf("0x%08x ", (unsigned int)(d + i));
		}

		if ((i % 8 == 0) && (i % 0x10 != 0))
			printf("  ");

		printf("%02x ", d[i]);
	}
	printf("\n");
}

int main(int argc,char argv[])
{
	int i = 0;
	char *dev = "eth0";
	char errbuf[PCAP_ERRBUF_SIZE];
	struct pcap_pkthdr h;
	const u_char *packet = NULL;
	struct ether_header *eptr;
	pcap_t *p;
	int snaplen = BUFSIZ;
	int promice = 1;
	int to_ms = 0;

#if 0
	// grab a device to peak into
	dev=pcap_lookupdev(errbuf);	
	if (dev == NULL)
	{
		fprintf(stderr, "pcap_lookupdev: %s\n", errbuf);
		exit(-1);
	}
	printf("DEV: %s\n",dev);
#endif

	// open the device
	p = pcap_open_live(dev, snaplen, promice, to_ms, errbuf);
	if (p == NULL)
	{
		fprintf(stderr, "pcap_open_live: %s\n", errbuf);
		exit(-1);
	}

	// grab a packet form the descr
	while (1)
	{
		packet = pcap_next(p, &h);
		printf("%d\n", h.len);
		print_packet_bytes(packet, h.len);
		printf("\n");
	}

#if 0
	printf("\n");
	printf("%d\n", h.len);
	do
	{
		printf("%02x ", *packet++);
		++i;
		if (i % 8 == 0)
			printf("     ");
		if(i % 16 == 0)
			printf("\n");
	} while (*packet != '\n');
	printf("\n");
#endif

	pcap_close(p);	//close the device
	return 0;
}
